const app = getApp();

// pages/mine/address.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    tel: '',
    latitude:'',
    longitude: '',
    address: '',
    specific_address: '',
    tag:['家','公司','学校','其他'],
    currentTab:0,
    tagName: '',
    address: '',
    is_default: 0,
    addressId:''
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var addressId = options.id
    if (addressId && addressId != undefined){
      app.wxPost(app.globalData.host + '/info/getAddressById', {
        id: addressId
      }).then(res => {
        var currentTab = 0;
        if (res.tip == '家') {
          currentTab = 0;
        } else if (res.tip == '公司') {
          currentTab = 1;
        } else if (res.tip == '学校') {
          currentTab = 2;
        } else {
          currentTab = 3;
        }
        that.setData({
          name: res.name,
          tel: res.tel,
          latitude: res.latitude,
          longitude: res.longitude,
          address: res.address,
          specific_address: res.specific_address,
          currentTab: currentTab,
          tagName: res.tip,
          address: res.address,
          is_default: res.is_default,
          addressId: addressId
        })
      })
    }
  },
  onShow:function() {
    this.setData({
      latitude: app.globalData.address_latitude,
      longitude: app.globalData.address_longitude,
      address: app.globalData.address_positioName
    })
  },

  //响应点击导航栏
  tagTap: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    var tagName = '';
    if (idx == 0) {
      tagName = '家';
    } else if (idx == 1) {
      tagName = '公司';
    } else if (idx == 2) {
      tagName = '学校';
    } else {
      tagName = '其他';
    }
    that.setData({
      tagName: tagName
    })
    that.setData({
      currentTab: e.currentTarget.dataset.idx,
    })
  },
  //姓名
  nameInput: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  //手机号码
  telInput: function(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  //详细地址
  addressInput: function(e) {
    this.setData({
      specific_address: e.detail.value
    })
  },
  intoPosition: function(){
    wx.navigateTo({
      url: '../../index-online/position/position?type=1',
    })
  },
  checkChange:function(e){
    if (e.currentTarget.dataset.is_default == 0){
      this.setData({
        is_default: 1
      })
    } else {
      this.setData({
        is_default: 0
      })
    }
  },
  //添加收货地址
  address: function() {
    var name = this.data.name;
    var tel = this.data.tel;
    var address = this.data.address;
    var specific_address = this.data.specific_address;
    var latitude = this.data.latitude;
    var longitude = this.data.longitude;
    var tagName = this.data.tagName;
    var is_default = this.data.is_default;
    var addressId = this.data.addressId;
    if (name == '') {
      app.showToastNone('收货人不可为空', 2000);
      return false;
    }
    if (tel == '') {
      app.showToastNone('联系方式不可为空', 2000);
      return false;
    }
    if (address == '') {
      app.showToastNone('收货地址不可为空', 2000);
      return false;
    }
    if (addressId == ''){
      app.wxPost(app.globalData.host + '/info/addAddress', {
        name: name,
        tel: tel,
        latitude: latitude,
        longitude: longitude,
        address: address,
        specific_address: specific_address,
        tip: tagName,
        is_default: is_default
      }).then(res => {
        app.showToastNone('添加成功', 1000);
        setTimeout(function () {
          wx.navigateBack({
            url: '../../index-online/address/address',
          })
        }, 1000)
      })
    } else {
      app.wxPost(app.globalData.host + '/info/updateAddress', {
        name: name,
        tel: tel,
        latitude: latitude,
        longitude: longitude,
        address: address,
        specific_address: specific_address,
        tip: tagName,
        is_default: is_default,
        id: addressId
      }).then(res => {
        app.showToastNone('修改成功', 1000);
        setTimeout(function () {
          wx.navigateBack({
            url: '../../index-online/address/address',
          })
        }, 1000)
      })
    }
  },
})